﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SMP_QuanLySV.DAL;

namespace SMP_QuanLySV.BLL
{
    class BLLBangDiem
    {
        List<BangDiem> list;
        QuanLySVDataContext db;


        // constructor
        public BLLBangDiem()
        {
            this.list = new QuanLySVDataContext().BangDiems.ToList();
            this.db = new QuanLySVDataContext();
        }

        // kiem tra 1 diem co ton tai hay khong
        public bool checkExist(BangDiem temp)
        {
            foreach (BangDiem dt in list)
            {
                if (dt.ID.Equals(temp.ID))
                    return true;
            }
            return false;
        }

        // update thong tin diem
        public bool updateDiem(BangDiem update, int lan, Decimal diem)
        {
            try
            {
                var Diem = (from item in db.BangDiems
                            where update == item
                            select item).Single();
                if (lan == 1)
                {
                    Diem.DiemLan1 = diem;
                }
                else if (lan == 2)
                {
                    Diem.DiemLan2 = diem;
                }
                else Diem.DiemLan3 = diem;
                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // them diem moi vao database
        public bool insertDiem(BangDiem temp)
        {
            try
            {
                db.BangDiems.InsertOnSubmit(temp);

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // xoa diem
        public bool deleteDiem(BangDiem temp)
        {
            try
            {
                var dt = (from c in db.BangDiems
                          where c.ID == temp.ID
                          select c).First();
                BangDiem found = (BangDiem)dt;

                db.BangDiems.DeleteOnSubmit(found);

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // tra ve danh sach cac diem
        public List<BangDiem> returnListDiemByClass(string className, string namhoc, string hocky)
        {
            this.list = new QuanLySVDataContext().BangDiems.Where(x => x.MonHocID == className && x.NamHoc == namhoc && x.HocKy == hocky).ToList();
            return list;
        }

        // tra ve 1 diem theo ma diem
        public BangDiem retrieveByID(string ID)
        {
            this.list = new QuanLySVDataContext().BangDiems.ToList();
            foreach (BangDiem temp in list)
                if (temp.ID == ID)
                {
                    return temp;
                }
            return null;
        }

        public BangDiem retrieveByIDAndSVID(string ID, string SVID)
        {
            var temp = (from c in db.BangDiems
                        where c.ID == ID && c.SVID == SVID
                        select c).First();

            return (BangDiem)temp;
        }

        public BangDiem retrieveByIndex(int i)
        {
            this.list = new QuanLySVDataContext().BangDiems.ToList();
            return list[i];
        }

        // kiem tra thong tin 1 diem co hop le hay khong
        public bool kiemTra(BangDiem temp)
        {
            if (temp.ID == "") return false;
            return true;
        }
    }
}
